package ewa.big.controller;

import ewa.big.entities.Player;
import ewa.big.entities.RegisteredPlayerPool;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 * <b>Web Engineering - UE3</b>
 * <p>
 *
 * This class represents the login control of the "Mensch aergere Dich nicht" game.
 * 
 *  
 */
@ManagedBean(name = "lc")
@SessionScoped
public class LoginControl implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManagedProperty(value = "#{player}")
    private Player player;
    @ManagedProperty(value = "#{rpp}")
    private RegisteredPlayerPool rpp;
    @ManagedProperty(value = "#{mc}")
    private MaednControl mc;
    @ManagedProperty(value = "false")
    private boolean showLoginfailed;
    private String username;
    private String password;

    /** C'tor  Creates a new instance of LoginControl */
    public LoginControl() {
    } // end C'tor

    /**
     * The login method.
     * 
     * @return  the forward uri string.
     */
    public String login() {
        player = getRpp().getRegisteredPlayer(username, password);
        if (player != null) {
            setShowLoginfailed(false);

            mc = new MaednControl(player.getUsername());

            return "table";
        } else {
            setShowLoginfailed(true);
            return "index";
        }
    } // end login

    /**
     * Get the player.
     * 
     * @return the player
     */
    public Player getPlayer() {
        return player;
    } // end getPlayer

    /**
     * Set the player.
     * 
     * @param player    the player to set
     */
    public void setPlayer(Player player) {
        this.player = player;
    } // end setPlayer

    /**
     * Get the username.
     * 
     * @return the username
     */
    public String getUsername() {
        return username;
    } // end getUsername

    /**
     * Set the username.
     * 
     * @param username    the name to set
     */
    public void setUsername(String username) {
        this.username = username;
    } // end setUsername

    /**
     * Get the password.
     * 
     * @return the password
     */
    public String getPassword() {
        return password;
    } // end getPassword

    /**
     * Set the password.
     * 
     * @param password    the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    } // end setPassword

    /**
     * Get the registered player pool.
     * 
     * @return the rpp
     */
    public RegisteredPlayerPool getRpp() {
        return rpp;
    } // end getRpp

    /**
     * Set the registered player pool.
     * 
     * @param rpp    the rpp to set
     */
    public void setRpp(RegisteredPlayerPool rpp) {
        this.rpp = rpp;
    } // end setRpp

    /**
     * Checks if the login failed.
     * 
     * @return the showLoginfailed
     */
    public boolean isShowLoginfailed() {
        return showLoginfailed;
    } // end isShowLoginfailed

    /**
     * Set the login failed flag.
     * 
     * @param showLoginfailed    the showLoginfailed to set
     */
    public void setShowLoginfailed(boolean showLoginfailed) {
        this.showLoginfailed = showLoginfailed;
    } // end setShowLoginfailed

    /**
     * Get the MaednControl.
     * 
     * @return the mc
     */
    public MaednControl getMc() {
        return mc;
    } // end getMc

    /**
     * Set the MaednControl.
     * 
     * @param mc    the mc to set
     */
    public void setMc(MaednControl mc) {
        this.mc = mc;
    } // end setMc
    
} // end of class LoginControl
